<?php

namespace App\Http\Controllers\API;

use App\Http\Resources\ExhibitorInformationResource;
use App\Models\ExhibitorInformation;
use App\Models\ExhibitorMessage;
use App\Models\ExhibitorMessageTask;
use App\Models\ExhibitorMessageTaskExpire;
use App\Models\ExhibitorUser;
use App\Models\MessageProcessCount;
use App\Services\AliSmsService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ExhibitorIndexController extends Controller
{


    public function index(Request $request)
    {

        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_information_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_message_id) {
            return $this->failure('登录超时，重新登录');
        }

        $exhibitor_message = ExhibitorMessage::query()->find(1);

        $data = [];
        $exhibitor_message_task = ExhibitorMessageTask::query()->where('exhibitor_message_id', $exhibitor_message_id)->get();
        $contents = [];
        foreach ($exhibitor_message_task as $item) {
            $type_id = $item->type_id;
            $contents[$type_id][] = $item->contents;
        }
        if ($exhibitor_message) {
            $data['exhibitor_message_id'] = $exhibitor_message->id;
            $data['title'] = $exhibitor_message->title;
            $data['exhibitor_time'] = $exhibitor_message->exhibitor_time;
            $data['addres'] = $exhibitor_message->addres;
            $data['contacts'] = $exhibitor_message->contacts;
            $data['matters_needing_attention'] = $exhibitor_message->matters_needing_attention;
            $data['contact_phone'] = $exhibitor_message->contact_phone;
            $data['e_mail'] = $exhibitor_message->e_mail;
            $data['contents'] = $contents;

        }

        $message_process_count1 = MessageProcessCount::query()->where('model_type_id', 1)->where('certificates_exhibitor_information_id', $exhibitor_information_id)->first();
        $exhibitor_message_task_expire = ExhibitorMessageTaskExpire::query()->where('type_id', 1)->where('exhibitor_message_id', $exhibitor_message_id)->first();
        if ($message_process_count1) {
            $message_process_count_temp1['model_type_id'] = 1;
            $message_process_count_temp1['title'] = '安全责任书';
            $message_process_count_temp1['is_completed'] = 1;
            $message_process_count_temp1['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));
        } else {
            $message_process_count_temp1['model_type_id'] = 1;
            $message_process_count_temp1['title'] = '安全责任书';
            $message_process_count_temp1['is_completed'] = 0;
            $message_process_count_temp1['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));
        }
        $message_process_count_array[] = $message_process_count_temp1;

        $message_process_count2 = MessageProcessCount::query()->where('model_type_id', 2)->where('certificates_exhibitor_information_id', $exhibitor_information_id)->first();
        $exhibitor_message_task_expire = ExhibitorMessageTaskExpire::query()->where('type_id', 2)->where('exhibitor_message_id', $exhibitor_message_id)->first();

        if ($message_process_count2) {
            $message_process_count_temp2['model_type_id'] = 2;
            $message_process_count_temp2['title'] = '会刊内容';
            $message_process_count_temp2['is_completed'] = 1;
            $message_process_count_temp2['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));

        } else {
            $message_process_count_temp2['model_type_id'] = 2;
            $message_process_count_temp2['title'] = '会刊内容';
            $message_process_count_temp2['is_completed'] = 0;
            $message_process_count_temp2['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));

        }
        $message_process_count_array[] = $message_process_count_temp2;


        $message_process_count_count3 = MessageProcessCount::query()->where('model_type_id', 3)->where('certificates_exhibitor_information_id', $exhibitor_information_id)->get();
        $exhibitor_message_task_expire = ExhibitorMessageTaskExpire::query()->where('type_id', 3)->where('exhibitor_message_id', $exhibitor_message_id)->first();
        $exhibitor_information = ExhibitorInformation::query()->find($exhibitor_information_id);
        $need_to_submit = 0;//需要提交  papers
        $exhibitor_message = ExhibitorMessage::query()->where('status', 1)->first();

        if ($exhibitor_message) {
            $need_to_submit = $exhibitor_information->papers;
            $data['booth_number'] = $exhibitor_information->booth_number;
        }


        if ($message_process_count_count3) {
            $submit_number = $message_process_count_count3->count('id');//已提交
            $message_process_count_temp3['model_type_id'] = 3;
            $message_process_count_temp3['title'] = '展商证件申请';
            $message_process_count_temp3['need_to_submit'] = $need_to_submit;//需要提交
            $message_process_count_temp3['submit_number'] = $submit_number;//已提交
            $remaining_to_submit = 0;
            if ($submit_number > $need_to_submit) {
                $remaining_to_submit = 0;
            } else {
                $remaining_to_submit = $need_to_submit - $submit_number;
            }
            $message_process_count_temp3['remaining_to_submit'] = $remaining_to_submit;//还可提交
            if ($submit_number == $need_to_submit) {
                $message_process_count_temp3['is_completed'] = 1;
            } else {
                $message_process_count_temp3['is_completed'] = 0;
            }

            $message_process_count_temp3['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));

        } else {
            $message_process_count_temp3['model_type_id'] = 3;
            $message_process_count_temp3['title'] = '展商证件申请';
            $message_process_count_temp3['need_to_submit'] = $need_to_submit;//需要提交
            $message_process_count_temp3['submit_number'] = 0;//已提交
            $message_process_count_temp3['remaining_to_submit'] = $need_to_submit;//还可提交
            $message_process_count_temp3['is_completed'] = 0;
            $message_process_count_temp3['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));
        }
        $message_process_count_array[] = $message_process_count_temp3;
        $exhibitor_message_task_expire = ExhibitorMessageTaskExpire::query()->where('type_id', 4)->where('exhibitor_message_id', $exhibitor_message_id)->first();
        $message_process_count_temp4['model_type_id'] = 4;
        $message_process_count_temp4['title'] = '特装展位报馆';
        $message_process_count_temp4['expire_time'] = date('Y-m-d', strtotime($exhibitor_message_task_expire->expire_time));

        $message_process_count_array[] = $message_process_count_temp4;

        $data['message_process_count_array'] = $message_process_count_array;


        if ($data) {
            return $this->success($data);
        } else {
            return $this->failure('失败');
        }
    }

}
